package com.hspedu.web.requestparam;

import com.hspedu.web.entity.Master;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RequestMapping("/vote")
@Controller
public class VoteHandler {
    @RequestMapping(value = "/vote01")
    public String test01(@RequestParam(value = "name", required = false)
                         String username) {
        System.out.println("得到的 username= " + username);
//返回到一个结果
        return "success";
    }

    @RequestMapping(value = "/vote02")
    public String test02(@RequestHeader("Accept-Encoding") String ae,
                         @RequestHeader("Host") String host) {
        System.out.println("Accept-Encoding =" + ae);
        System.out.println("Host =" + host);
//返回到一个结果
        return "success";
    }

    @RequestMapping(value = "/vote03")
    public String test03(Master master) {
        System.out.println("master="+master);
        System.out.println("主人信息= " + master);
//返回结果
        return "success";
    }

    @RequestMapping(value = "/vote04")
    public String test04(HttpServletRequest request,
                         HttpServletResponse response,
                         HttpSession httpSession) {
        HttpSession session = request.getSession();
        System.out.println("httpSession="+httpSession);
        System.out.println("session="+session);
        System.out.println("name= " + request.getParameter("username"));
        System.out.println("pwd= " + request.getParameter("pwd"));
//返回结果
        return "success";
    }


    @RequestMapping(value = "/vote05")
    public String test05(Master master, HttpServletRequest request, HttpServletResponse response) {
        //老韩解读
        //1. springmvc 会自动把获取的 model 模型，放入到 request 域中，名字就是 master
        //2. 也可以手动将 master 放入到 request
        //request.setAttribute("master", master);
        request.setAttribute("address", "北京");
        master.setName("nono");
        //返回到一个结果
        return "vote_ok";
    }

    @RequestMapping(value = "/vote06")
    public String test06(Map<String, Object> map, Master master) {
        System.out.println("=====test06()=====~");
        //老韩解读
        //1. springMVC 会将 map 的 k-v 放入到 request 域对象
        map.put("master", null);
        map.put("address", "beijing");
        //返回到一个结果
        return "vote_ok";
    }


    @RequestMapping(value = "/vote07")
    public ModelAndView test07(Master master) {
        System.out.println("======test07()======");
        //创建一个 ModelAndView 对象
        ModelAndView modelAndView = new ModelAndView();
        //下面这句话就等价于将 master 对象放入到 request 域中，属性名 “master”
        modelAndView.addObject("master", master);
        //modelAndView.addObject("master",null);
        modelAndView.addObject("address", "shanghai");
        //指定要跳转的视图
        modelAndView.setViewName("vote_ok");
        System.out.println("modelAndView="+modelAndView);
        //返回结果
        return modelAndView;
    }

    @RequestMapping(value = "/vote08")
    public String test08(Master master, HttpSession httpSession) {

        httpSession.setAttribute("master",master);
        httpSession.setAttribute("address","广州");
        return "vote_ok";
    }
    @ModelAttribute
    public void init(){
        System.out.println("init()完成准备工作");
    }

}
